<script setup lang="ts">
import { ref } from 'vue'
import { useDict } from './hook'

import { useRenderIcon } from '@/components/ReIcon/src/hooks'
import { useTableHeight } from '@/hooks/useTableHeight'
import DialogForm from './components/dialog-form.vue'
import Search from '@iconify-icons/ep/search'
import Refresh from '@iconify-icons/ep/refresh'
import AddFill from '@iconify-icons/ri/add-circle-line'
import Delete from '@iconify-icons/ri/delete-bin-2-line'
import Download from '@iconify-icons/ri/download-line'
defineOptions({
  name: 'Dict',
})

const formRef = ref()
const {
  form,
  title,
  dictId,
  loading,
  columns,
  dataList,
  multiple,
  dateRange,
  pagination,
  queryParams,
  showSelection,
  formDialogVisible,
  sys_normal_disable,
  onSearch,
  resetForm,
  handleAdd,
  handleUpdate,
  handleDelete,
  handleExport,
  handleRefreshCache,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
} = useDict()
const { tableHeight, tableRef } = useTableHeight()
</script>

<template>
  <div class="main table-view-layout">
    <el-form
      ref="formRef"
      :inline="true"
      :model="queryParams"
      class="bg-el-bg w-[99/100] pl-8 pt-4"
    >
      <el-form-item label="字典名称：" prop="dictName">
        <el-input
          v-model="queryParams.dictName"
          placeholder="请输入字典名称"
          clearable
          class="!w-[200px]"
          @keyup.enter="onSearch"
        />
      </el-form-item>
      <el-form-item label="字典类型：" prop="dictType">
        <el-input
          v-model="queryParams.dictType"
          placeholder="请输入字典类型"
          clearable
          class="!w-[180px]"
          @keyup.enter="onSearch"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="字典状态"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in sys_normal_disable"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" style="width: 308px">
        <el-date-picker
          v-model="dateRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="loading"
          @click="onSearch"
        >
          搜索
        </el-button>
        <el-button :icon="useRenderIcon(Refresh)" @click="resetForm(formRef)"> 重置 </el-button>
      </el-form-item>
    </el-form>
    <PureTableBar title="字典管理">
      <template #buttons>
        <el-button type="primary" :icon="useRenderIcon(AddFill)" @click="handleAdd">
          新增
        </el-button>
        <el-button
          type="primary"
          :icon="useRenderIcon(Delete)"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
        <el-button type="primary" :icon="useRenderIcon(Refresh)" @click="handleRefreshCache">
          刷新缓存
        </el-button>
        <el-button type="primary" :icon="useRenderIcon(Download)" @click="handleExport">
          导出
        </el-button>
      </template>
      <template #table>
        <pure-table
          ref="tableRef"
          border
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="loading"
          :max-height="tableHeight"
          :checkList="showSelection ? ['勾选列'] : []"
          :data="dataList"
          :columns="columns"
          :pagination="pagination"
          :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)',
          }"
          @selection-change="handleSelectionChange"
          @page-size-change="handleSizeChange"
          @page-current-change="handleCurrentChange"
        >
          <template #operation="{ row }">
            <el-button class="reset-margin" link type="primary" @click="handleUpdate(row)">
              修改
            </el-button>
            <el-button class="reset-margin" link type="primary" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
    <DialogForm
      v-model:visible="formDialogVisible"
      :title="title"
      :data="form"
      :dictId="dictId"
      @refresh="onSearch"
    />
  </div>
</template>

<style scoped lang="scss">
:deep(.el-dropdown-menu__item i) {
  margin: 0;
}
</style>
